

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client
{
	public static String waiting_connect() throws IOException
	{
		String adr_serv = null;
		ServerSocket socket_serveur;
		
		try
		{
			String ret = null;
			socket_serveur = new ServerSocket(2010);
			Socket serviceSocket = socket_serveur.accept();
			System.out.println("Connexion au serv, getting ip");

			adr_serv = serviceSocket.getInetAddress().toString();
			System.out.println("L'ip est : "+ adr_serv);
			socket_serveur.close();
			serviceSocket.close();
		}
		catch (Exception e)
		{
			System.out.println("Erreur w8 connect");
			return null;
		}
		return adr_serv;
	}
	
	public static void init_client() throws IOException
	{
		Socket socket = null;
		try
		{  
			String str = null;
			String s1 = null;
			str = waiting_connect();
			if (str != null)
			{
				if(str.charAt(0) == '/')
				{
					s1 = str.substring(1, str.length());
					System.out.println("Chaine resultat : " + s1);
				}
					socket = new Socket(InetAddress.getByName(s1), 2011); 
		//	socket = new Socket(InetAddress.getByName("192.168.1.12"), 2011);
					EnvoiFichier.SendFile("D:\\\\killer.exe", socket);
					socket.close();
			}
		}
		catch (UnknownHostException e) 
		{
			System.out.println("Echec creation client");
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			System.out.println("Echec creation client");
		}
	}
}
